using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Controllers
{
    #region using
    using System.Web.Mvc;
    using TemplateWeb.Models.ViewModels;
    using TemplateWeb.Models;
    using TemplateWeb.Models.Repositories;
    using TemplateWeb.Helpers;
    using System.Web.Security;
    #endregion
    public partial class AdminController : BaseController
    {
        public ActionResult SelectQuanLy(string role)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_QuanLy,
                Role = role
            };
            return View(CreateViewModel(data));
        }

        public ActionResult SelectQuanLyPaging(string role, int page = 1)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_QuanLy,
                Role = role,
                CurrentPage = page
            };
            return PartialView("Templates/TH_ListQuanLy", CreateViewModel(data));
        }

        [AcceptVerbs(HttpVerbs.Delete)]
        public JsonResult DeleteQuanLy(Guid id, string role)
        {
            return Json(new
            {
                Success = _repQuanLy.Delete(id),
                RecordCount = _repQuanLy.GetTotalPage(role),
                DeleteId = id
            });
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateQuanLy(FormCollection forms)
        {
            string username = forms.GetValues("QuanLy_UserName")[0];
            string email = forms.GetValues("QuanLy_Email")[0];
            string id = forms.GetValues("QuanLy_ID")[0];
            string hoten = forms.GetValues("QuanLy_HoTen")[0];
            string phone = forms.GetValues("QuanLy_Phone")[0];
            string nickname = forms.GetValues("QuanLy_NickName")[0];
            string currentpage = forms.GetValues("QuanLy_CurrentPage")[0];
            var user = Membership.GetUser(username);
            if (email != user.Email)
            {
                user.Email = email;
                Membership.UpdateUser(user);
            }
            var QuanLy = _repQuanLy.SelectByID(Guid.Parse(id));
            if (QuanLy == null)
            {
                QuanLy = new QuanLy
                {
                    ID = Guid.Parse(id),
                    HoTen = hoten
                };
                DataContext.Instance.QuanLy.AddObject(QuanLy);
                DataContext.Instance.SaveChanges();
            }
            else
            {
                QuanLy.HoTen = hoten;
                _repQuanLy.Save();
            }
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_QuanLy,
                CurrentPage = int.Parse(currentpage)
            };
            return PartialView("Templates/TH_ListQuanLy", CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertQuanLy(FormCollection forms)
        {
            string errorText = "";
            var QuanLyInfo = new AddQuanLyViewModel
            {
                Info = new QuanLyInfo
                {
                    BaseInfo = new QuanLyBaseInfo
                    {
                        UserName = forms.GetValues("QuanLy_UserName")[0],
                        Password = forms.GetValues("QuanLy_Password")[0],
                        Email = forms.GetValues("QuanLy_Email")[0],
                        Role = forms.GetValues("QuanLy_Role")[0]
                    }
                }
            };
            var result = _repQuanLy.Insert(QuanLyInfo, ref errorText);
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_QuanLy,
                Role = forms.GetValues("QuanLy_OwnerRole")[0]
            };
            return Json(new
            {
                Success = result,
                Message = "A new item has been added!",
                PartialViewHtml = RenderPartialViewToString("Templates/TH_ListQuanLy", CreateViewModel(data))
            });
        }

        public ActionResult DetailOfQuanLy(Guid id)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_QuanLy,
                GuidID = id
            };
            return View(CreateViewModel(data));
        }

    }
}